//
//  ViewController.m
//  小鱼
//
//  Created by situ_ios_004 on 15/9/16.
//  Copyright (c) 2015年 situ_ios_004. All rights reserved.
//

#import "ViewController.h"
#define IMAGE_COUNT 2
@interface ViewController ()
{
    CALayer *_layer;
    int _index;
    NSMutableArray *_images;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //设置背景
    self.view.layer.contents=(id)[UIImage imageNamed:@"beijing.jpg"].CGImage;
    self.view.contentMode= UIViewContentModeScaleAspectFit;
    //创建图像显示图层
    _layer=[[CALayer alloc]init];
    _layer.bounds=CGRectMake(0, 0, 87, 32);
    _layer.position=CGPointMake(350, 284);
    [self.view.layer addSublayer:_layer];
//
//    //由于鱼的图片在循环中会不断创建，而10张鱼的照片相对都很小
//    //与其在循环中不断创建UIImage不如直接将10张图片缓存起来
    _images=[NSMutableArray array];
    for (int i=0; i<2
         ; ++i) {
        NSString *imageName=[NSString stringWithFormat:@"%d.jpg",i];
        UIImage *image=[UIImage imageNamed:imageName];
        [_images addObject:image];
    }
    
    //定义时钟对象
    CADisplayLink *displayLink=[CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
    //添加时钟对象到主运行循环
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    //创建动画
//[self translationAnimation];
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=touches.anyObject;
    CGPoint location= [touch locationInView:self.view];
    
    [self translatonAnimation:location];
    
}
#pragma mark 每次屏幕刷新就会执行一次此方法(每秒接近60次)
-(void)step{
    //定义一个变量记录执行次数
    static int s=0;
    //每秒执行6次
    if (++s%10==0) {
        UIImage *image=_images[_index];
        _layer.contents=(id)image.CGImage;//更新图片
        _index=(_index+1)%IMAGE_COUNT;
    }
}
-(void)translatonAnimation:(CGPoint)location{
    //1.创建关键帧动画并设置动画属性
    CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //2.设置关键帧,这里有四个关键帧
    NSValue *key1=[NSValue valueWithCGPoint:_layer.position];//对于关键帧动画初始值不能省略
    NSValue *key2=[NSValue valueWithCGPoint:CGPointMake(80, 220)];
    NSValue *key3=[NSValue valueWithCGPoint:CGPointMake(45, 230)];
    NSValue *key4=[NSValue valueWithCGPoint:CGPointMake(55, 300)];
    NSArray *values=@[key1,key2,key3,key4];
    keyframeAnimation.values=values;
    //设置其他属性
    keyframeAnimation.duration=10.0;

    keyframeAnimation.delegate=self;
    [keyframeAnimation setValue:[NSValue valueWithCGPoint:location] forKey:@"aa"];
   // keyframeAnimation.beginTime=CACurrentMediaTime()+2;//设置延迟2秒执行
        //3.添加动画到图层，添加动画后就会执行动画
    [_layer addAnimation:keyframeAnimation forKey:@"KCKeyframeAnimation_Position"];
    
}
#pragma mark 动画结束
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    //开启事务
    [CATransaction begin];
    //禁用隐式动画
    [CATransaction setDisableActions:YES];
    _layer.position=[[anim valueForKey:@"aa"] CGPointValue];
    //提交事务
    [CATransaction commit];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
